<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/16
 * Time: 9:25
 */

namespace app\cms\admin;


use app\cms\model\_article;
use app\system\admin\Admin;
use app\system\admin\User;
use app\system\model\SystemArticle;
use app\system\model\SystemModule;
use think\Db;
use think\facade\Cookie;
use think\Image;
use think\Request;

class Article extends Admin
{

    protected $saveRule = [
        "size" => 2 * 1024 * 1024,
        "ext" => ["bmp","jpeg","png","jpg"]
    ];
    protected $saveWater = "%s---%s";


    /**
     * 新闻页面遍历
     * @param string $group
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $page   = $this->request->param('page/d', 1);
            $limit  = $this->request->param('limit/d', 15);

            $data["data"] = Db::table("hisi_article")->page($page)->limit($limit)->select();

            $data['count']  = Db::table("hisi_article")->count('postId');

            foreach ($data["data"] as $key => $value) {
                $data["data"][$key]["imgSrc"] = substr($data["data"][$key]["imgSrc"], strrpos($data["data"][$key]["imgSrc"], '/')+1);
                $data["data"][$key]["avatar"] = substr($data["data"][$key]["avatar"], strrpos($data["data"][$key]["avatar"], '/')+1);
            }
            $data['code'] = 0;
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * 文章增加
     * @return mixed|string|void
     */
    public function add()
    {
        if ($this->request->isPost()) {

            $data = $this->request->post();

            $postIdArr = Db::table("hisi_article")->column("postId");
            if (in_array($data["postId"],$postIdArr)) {
                return $this->error("该postId已存在");
            }

            $data = $this->deal_data($data);

            $result = Db::table("hisi_article")->insert($data);

            if ($result){
                return $this->success("成功");
            } else {
                return $this->error("失败");
            }
        }
        return $this->fetch('add');
    }

    /**
     * 删除操作
     * @return mixed|void
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        $id = $this->request->param('postId/a');

        //$result = Db::table("swiper")->where("postId",intval($id[0]))->update(["delete_time" => time()]);
        $result = Db::table("hisi_article")->where("postId",intval($id[0]))->delete();

        if ($result) {
            return $this->success('删除成功');
        }

        return $this->error("删除失败");
    }

    public function edit()
    {
        $id = $this->request->param('postId/a');
        $data = Db::table("hisi_article")->where("postId",intval($id[0]))->find();
        if ($this->request->isPost()) {
            $newData = $this->request->post();

            $newData = $this->deal_data($newData);

            $id = intval($newData["postId"]);

            $modle = new SystemArticle();

            $result = $modle::where("postId",$id)->update($newData);

            if ($result) {
                return $this->success('修改成功');
            }

            return $this->error("修改失败");
        }
        $data["imgSrc"] = "http://127.0.0.1/niege/myapp/first/second/images" ."/" .  substr($data["imgSrc"], strrpos($data["imgSrc"], '/')+1);;
        $data["avatar"] = "http://127.0.0.1/niege/myapp/first/second/images/avatar" ."/" .  substr($data["avatar"], strrpos($data["avatar"], '/')+1);;
        $data["detail"] = htmlspecialchars_decode($data["detail"]);

        $this->assign("data",$data);
        return $this->fetch("edit");
    }

    /**
     * 新闻图片上传
     * @param Request $request
     * @return \think\response\Json
     */
    public function uploadFile(Request $request)
    {
        if (!empty($request->file())) {
            $image = $request->file()["file"];
            $info = $image->move("D:\phpstudy\PHPTutorial\WWW\\niege\myapp\\first\second\images");
        }

        $data["imgSrc"] = substr($info->getPathname(), strrpos($info->getPathname(), '\\')+1);
        $data["imgSrc"] = "http://127.0.0.1/niege/myapp/first/second/images" ."/" .  $data["imgSrc"];

        return json(["code" => 1 , "message" => "上传成功", "data" => $data["imgSrc"]]);
    }

    /**
     * 头像上传
     * @param Request $request
     * @return \think\response\Json
     */
    public function uploadAvatarImg(Request $request)
    {
        if (!empty($request->file())) {
            $image = $request->file()["file"];
            $info = $image->move("D:\phpstudy\PHPTutorial\WWW\\niege\myapp\\first\second\images\avatar");
        }

        $data["imgSrc"] = substr($info->getPathname(), strrpos($info->getPathname(), '\\')+1);
        $data["imgSrc"] = "http://127.0.0.1/niege/myapp/first/second/images/avatar" ."/" .  $data["imgSrc"];

        return json(["code" => 1 , "message" => "上传成功", "data" => $data["imgSrc"]]);
    }

    public function deal_data($data){
        $date = date("F d Y",time());
        $data["date"] = $date;

        $time = time();
        if ($time > (24*60*60)) {
            $rows["time"] =  get_data($time);
        } else {
            $rows["time"] =  get_time($time);
        }
        $data["dateTime"] = $rows["time"];

        $data['detail'] = htmlspecialchars_decode($data['detail']);
        $data['detail'] = str_replace("&̲nbsp;","",$data['detail']);
        $data['detail'] = strip_tags($data['detail']);

        $data["imgSrc"] = "/images/" . substr($data["imgSrc"], strrpos($data["imgSrc"], '/') +1 );
        $data["avatar"] = "/images/avatar/" . substr($data["avatar"], strrpos($data["avatar"], '/') +1 );

        $data["content"] = substr($data["detail"],0,30);

        return $data;
    }
}


function get_time($value) {
    if ($value > 3600) {
        return ceil($value/3600) . "小时前";
    } else {
        return ceil($value/60) . "分钟前";
    }
}

function get_data($value) {
    $num =  ceil(intval($value)/(24*60*60));
    if ($num >= 3) {
        $num = 3;
    }
    return $num ."天前";
}

